package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Pharmacyproduct;

@Repository(value = "pharmacyProductDao")
@Transactional(readOnly = false)
public class PharmacyProductDaoImpl implements PharmacyProductDao {

	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	
	public List<Pharmacyproduct> getListPharmacyProduct() {
		TypedQuery<Pharmacyproduct> q = em.createNamedQuery("Pharmacyproduct.findAll",Pharmacyproduct.class);
    	return q.getResultList();
	}

	public List<Pharmacyproduct> getListPharmacyProduct(boolean active) {
		TypedQuery<Pharmacyproduct> q = em.createNamedQuery("Pharmacyproduct.findByActive",Pharmacyproduct.class);
    	q.setParameter("active",active);
    	return q.getResultList();
	}

	public List<Pharmacyproduct> getListPharmacyProductByProviderId(Integer providerId) {
		TypedQuery<Pharmacyproduct> q = em.createNamedQuery("Pharmacyproduct.findByProviderId",Pharmacyproduct.class);
    	q.setParameter("providerId",providerId);
    	return q.getResultList();
	}

	public List<Pharmacyproduct> getListPharmacyProductByProviderId(Integer providerId, boolean active) {
		TypedQuery<Pharmacyproduct> q = em.createNamedQuery("Pharmacyproduct.findByProviderIdByActive",Pharmacyproduct.class);
    	q.setParameter("providerId",providerId);
    	q.setParameter("active",active);
    	return q.getResultList();
	}

	public List<Pharmacyproduct> getListPharmacyProductByProductId(Integer productId) {
		TypedQuery<Pharmacyproduct> q = em.createNamedQuery("Pharmacyproduct.findByProductId",Pharmacyproduct.class);
    	q.setParameter("productId",productId);
    	return q.getResultList();
	}

	public List<Pharmacyproduct> getListPharmacyProductByProductId(Integer productId, boolean active) {
		TypedQuery<Pharmacyproduct> q = em.createNamedQuery("Pharmacyproduct.findByProductIdByActive",Pharmacyproduct.class);
    	q.setParameter("productId",productId);
    	q.setParameter("active",active);
    	return q.getResultList();
	}

	public List<Pharmacyproduct> getListPharmacyProductByPharmacyId(Integer pharmacyId) {
		TypedQuery<Pharmacyproduct> q = em.createNamedQuery("Pharmacyproduct.findByPharmacyId",Pharmacyproduct.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	return q.getResultList();
	}

	public List<Pharmacyproduct> getListPharmacyProductByPharmacyId(Integer pharmacyId, boolean active) {
		TypedQuery<Pharmacyproduct> q = em.createNamedQuery("Pharmacyproduct.findByPharmacyIdByActive",Pharmacyproduct.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("active",active);
    	return q.getResultList();
	}

	public Pharmacyproduct findPharmacyProductByPharmacyIdByProviderIdByProductId(Integer pharmacyId,Integer providerId, Integer productId) {
		Pharmacyproduct res = null;
    	Query  q = em.createNamedQuery("Pharmacyproduct.findByPharmacyIdByProviderIdByProductId");
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("providerId",providerId);
    	q.setParameter("productId",productId);
    	if(q.getResultList().size()==1)
    		res=(Pharmacyproduct)q.getSingleResult();
    	return res;
	}

	public void update(Pharmacyproduct pharmacyProduct) {
		em.merge(pharmacyProduct);
	}

	public void add(Pharmacyproduct pharmacyProduct) {
		em.persist(pharmacyProduct);
	}

}
